package com.endava.exceptions.string_reverse;

import java.util.logging.Level;
import java.util.logging.Logger;

public class StringReverseExceptions {

	/**
	 * Reverse a String of characters.
	 * 
	 * @param text
	 *            the String to be reversed
	 * @return the reversed String
	 * @throws NullPointerException
	 */
	private static String reverse(String text) throws NullPointerException {

		String reverse = new String();
		try {
			if (text.length() != 0) {
				reverse = new StringBuilder(text).reverse().toString();
			} else {
				throw new Exception();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new NullPointerException();
		}
		return reverse;

	}

	public static void main(String[] args) {

		Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, "App started.");

		String text = "";
		String revert = "";

		try {
			revert = reverse(text);
			System.out.println(revert);
		} catch (NullPointerException npe) {
			// System.out.println("Empty: " + npe.toString());
			Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.SEVERE, "Your text to reverse is empty !");
		} finally {
			Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, "You are in finally branch.");
		}
	}

}
